from helpFunc import *
import os,time,json
import sys
from collections import deque
from conns import *

runningPath = os.path.split(os.path.realpath(__file__))[0]
print (runningPath)      
sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
sock.bind(('0.0.0.0',server_port))
sock.listen(10)

def work(data):
    w = structWrapper(data)
    k = w.readArr(6)
    if k!=con_key:
        return b''
    k  = w.readArr(6)
    maxConn = w.readDWord()
    cachePeerConn = w.readDWord()
    ssPort = w.readDWord()
    oneLen = w.readDWord()
    tLen = w.readDWord()
    bufsize = w.readDWord()
    effTime = w.readDouble()
    inter = w.readDouble()
    port = w.readDWord()
    m = {}
    m['maxConn'] = maxConn
    m['cachePeerConn'] = cachePeerConn
    m['ssPort'] = ssPort
    m['oneLen'] = oneLen
    m['tLen'] = tLen
    m['bufsize'] = bufsize
    m['effTime'] = effTime
    m['inter'] = inter
    m['port'] = port
    m['key'] = k.decode()

    import base64,json
    j = json.dumps(m)
    s = base64.b64encode(j.encode())
    ex = sys.executable      
    cmd = 'nohup %s %s/conns.py %s >/dev/null &'%(ex,runningPath,s.decode())
    os.system(cmd)            
    print (cmd)    

while True:
    try:
        a,b = sock.accept()
        r = b''
        while len(r)!=100:
            r += a.recv(100)
        dd = work(r)
        a.close()
    except:
        print ('excp')
    


   
